TOTAL_GAMES = 7

import spyral
import pygame
import random

import Dic
import Instruction;
import GameInstruction;
import Image;
import Timer;
import GameOver;
import Health;
from games import game001;
from games import game002;
from games import game003;
from games import game004;
from games import game005;
from games import game006;
from games import game007;


class Story(spyral.scene.Scene):
    def __init__(self, player):
        spyral.scene.Scene.__init__(self)
        self.camera = spyral.director.get_camera()
        self.group = spyral.sprite.Group(self.camera)
        
        self.intro = True;
        self.started = False;
        self.gameNum = 0;
        

        self.games_played = [];
        
        self.player = player;
        self.player.setHealth('story')
        self.health = Health.Health(self.player);
        
        self.group.add(self.health);
        
        self.firstInstructions();
    
    def on_enter(self):
        bg = spyral.util.new_surface((960,640))
        bg.fill(Dic.colors['bg'])
        self.camera.set_background(bg)
        
        #game over test
        self.health.updateHealth();
        if (self.player.getHealth() == 0):
            spyral.director.replace(GameOver.GameOver(self.player));
        
        '''
        TEMP: replays all games sped up
        '''
        if (len(self.games_played) == TOTAL_GAMES):
            print 'speed up'
            self.games_played = [];
            self.player.incCurrentSpeed();
        
        self.gameinstr = False;
        self.count = 0;
          
    def render(self):
        self.group.draw()
        self.camera.draw()
        
    def update(self, tick):
        #input handler
        for event in pygame.event.get():
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_ESCAPE:
                    exit(0)
            if event.type == pygame.QUIT:
                spyral.director.pop();
        
        #intro instructions
        if (self.intro == True):
            self.count += 1;
            if (self.count == 155):
                self.count = 0;
                self.started = True;
                self.intro = False;
        
        #pick game disp game instructions and run game
        if (self.started == True):
            self.count += 1;
            if (self.gameinstr == False):
                self.gameNum = self.chooseGame();
                self.group.add(GameInstruction.GameInstruction(self.gameNum));
                self.gameinstr = True;
            elif (self.count == 51):
                self.playGame(self.gameNum);
   
        self.group.update()
                
    def firstInstructions(self):
        self.pig = Image.Image(Dic.images['olpig'], (50, 430));
        self.instr1 = Instruction.Instruction();
        self.group.add(self.pig, self.instr1);
        
    def chooseGame(self):
        played = True;
        while (played == True):
            game = random.randint(1, TOTAL_GAMES)
            if (0 != len(self.games_played)):
                played = False;
                for num in self.games_played:
                    if num == game:
                        played = True;
            else:
                return game;
        return game;
        
    
    def playGame(self, num):
        if (num == 1):
            self.games_played.append(1);
            self.game = game001.game001(self.player);
            spyral.director.push(self.game);
        elif (num == 2):
            self.games_played.append(2);
            self.game = game002.game002(self.player);
            spyral.director.push(self.game);
        elif (num == 3):
            self.games_played.append(3);
            self.game = game003.game003(self.player);
            spyral.director.push(self.game);
        elif (num == 4):
            self.games_played.append(4);
            self.game = game004.game004(self.player);
            spyral.director.push(self.game)
        elif (num == 5):
            self.games_played.append(5);
            self.game = game005.game005(self.player);
            spyral.director.push(self.game)
        elif (num == 6):
            self.games_played.append(6);
            self.game = game006.game006(self.player);
            spyral.director.push(self.game)
        elif (num == 7):
            self.games_played.append(7);
            self.game = game007.game007(self.player);
            spyral.director.push(self.game)